package org.sc.learn.server.user.service;

import org.sc.learn.server.user.entity.User;
import org.sc.learn.server.user.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * @author qianwx
 */
@Service
public class UserService {

  @Autowired
  private UserRepository userRepository;

  public User addUser(User user) {
    Assert.notNull(user, "用户不能为空");
    return userRepository.save(user);
  }

  public User findUser(long id) {
    Assert.isTrue(id > 0, "id不能为空");
    return userRepository.findOne(id);
  }

  public User findUser(String name) {
    Assert.hasLength(name, "姓名不能为空");
    return userRepository.findByUserName(name);
  }
}
